!(function ($, window, document, undefined) {

    var defaults = {};

    var Menu = function (element, options) {
        this.$this = element;
        this.$opts = options;
    };

    var tempUL_1 = '\
                <h5 class="sidebartitle">导航栏</h5>\
                <ul id="main-nav" class="nav nav-pills nav-stacked nav-bracket">\
                {0}\
                </ul>\
            ';
    var tempLI_1 = '\
                <li class="{0}" data-sid="{1}">\
                    <a href="javascript:void(0)">\
                        <i class="fa {2}"></i> \
                        <span>{3}</span>\
                    </a>\
                    <ul class="children">\
                    </ul>\
                </li>\
            ';

    var tempLI_2 = '\
                <li data-href=\"{0}\">\
                    <a href=\"javascript:void(0)\">\
                        <i class=\"fa fa-caret-right\"></i> \
                        {1}\
                    </a>\
                </li>\
            ';

    Menu.prototype = {
        constructor: Menu,
        // 加载并渲染一级菜单
        addPrimaryMenu: function () {
            var menus = ajaxJson('/scResource/listByCondition', JSON.stringify({
                type: 1,
                parentSid: '0'
            }));
            if (!menus) return;
            // 加载菜单并渲染
            var html = tempUL_1.format(
                $.map(menus, function (m) {
                    return tempLI_1.format('', m.sid, m.icon, m.name, '');
                }).join('')
            );
            console.log(html);
            this.$this.html(html);
        },
        // 一级菜单单击事件
        primaryMenuClick: function () {
            var e = this;
            var children = $('#main-nav').children('li');
            children.click(function () {
                var sid = $(this).attr('data-sid');
                // 判断是否选中同一个菜单
                if (sid == e.selected) return;
                // 移除“选中”样式
                children.removeClass('active');
                // 清空所有子菜单
                children.children('ul').empty();
                // 添加“选中项”样式
                $(this).addClass('active');
                // 加载子菜单
                var nodes = ajaxJson('/scResource/listByCondition', JSON.stringify({
                    type: 1,
                    parentSid: sid
                }));
                console.log(nodes);
                // 添加子菜单
                var nodesHtml = e.addNodes(nodes);
                $(this).children('ul').html(nodesHtml);
                // 子菜单单击事件
                $(this).children('ul').children('li').click(function () {
                    var url = $(this).attr('data-href');
                    $(window.document).find("#iframe-main").attr('src', url);
                });
                // 设置上次选中项
                e.selected = sid;
            });
        },
        // 添加并渲染二级菜单
        addNodes: function (nodes) {
            if (!nodes) return '';
            return $.map(nodes, function (m) {
                return tempLI_2.format(m.url, m.name);
            }).join('');
        }
    };

    $.fn.Menu = function (options) {
        var menu = new Menu(this, options);
        menu.addPrimaryMenu();
        menu.primaryMenuClick();
        return menu;
    };

    $.fn.Menu.Constructor = Menu;

})(jQuery, window, document);